import time

from public.action import *
from public.business import *
from public.testData import *
from public.tools import *

testCase = "dbshopST009_搜索退货单"

# 打开前台网址
firefox = openBrowser(url)
# 登陆
DBshopLogin(firefox, userName, password)
# 进入我的订单模块
firefox.find_element_by_link_text('我的订单').click()
# 点击交易完成
firefox.find_element_by_partial_link_text('交易完成').click()

if firefox.find_element_by_xpath('//span[text()="订单完成"]'):
    firefox.find_element_by_partial_link_text('订单详情').click()
    a = firefox.find_element_by_xpath('(//div[@class="box-hd"])[2]/h3').text
    ordernumber = reGetString(a, '订单编号：', '$')
    print(ordernumber)
    #保存订单编号
    rootPath=get_pro_rootpath()
    writeFile(rootPath+'\public\order002.text',ordernumber)
    ####
    firefox.find_element_by_link_text('退货申请').click()
    firefox.find_element_by_xpath('//input[@placeholder="输入订单编号"]').send_keys(ordernumber)
    firefox.find_element_by_xpath('//button').click()
    if firefox.find_element_by_xpath('//div[contains(text(),"暂无记录")]'):
        firefox.find_element_by_link_text('点此申请退货').click()
    firefox.find_element_by_id('order_sn').send_keys(ordernumber)  # 输入订单编号
    firefox.find_element_by_xpath('//button[text()="点击查询"]').click()  # 点击查询
    firefox.find_element_by_xpath('(//input[@name="refund_type"])[1]').click()  # 选择账户余额
    firefox.find_element_by_id('refund_info').send_keys('111')  # 输入退货原因
    firefox.find_element_by_xpath('//button[text()="申请退货"]').click()  # 输入申请退货
    time.sleep(3)
    firefox.switch_to.alert.accept()
# else:
#     print('没有已完成的订单')
firefox.quit()

# 打开后台网址
firefox = openBrowser(urlAdmin)
# 登陆
DBshopLoginAdmin(firefox)
# 进入二级菜单
DBshopEnterMenu2(firefox, '销售管理', '退货管理')
firefox.find_element_by_name('order_sn').send_keys(ordernumber)
firefox.find_element_by_xpath('(//select[@name="refund_type"])/option[2]').click()
firefox.find_element_by_xpath('//button[contains(text(),"搜索")]').click()
# 断言
try:
    firefox.find_element_by_link_text('处理退货')
    writeLog("%s 测试通过" % testCase)
except:
    writeLog("%s 测试失败" % testCase)
    getPhoto(firefox)
firefox.quit()
